package com.eastday.dao.impl;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.List;

import org.jdom.JDOMException;

import com.eastday.dao.impl.ConnectionBuilderDao;
import com.eastday.dao.ImportNewsDao;
import com.eastday.model.NewsInfoModel;

/**
 * 
 * @author dingxs
 *
 */
public class ImportNewsOracleDaoImpl extends ConnectionBuilderDao implements ImportNewsDao {

	public ImportNewsOracleDaoImpl()
	{
		super();
		try {
			this.conntoOracleDb();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		}
	}
	
	

	@Override
	public void importNews(List<NewsInfoModel> newsInfoModelList) {
		// TODO Auto-generated method stub
		try {
			String sqlStr="";
			for(NewsInfoModel newsInfoModel:newsInfoModelList)
			{
				PreparedStatement psforinsert = null;
				try {
					sqlStr = "INSERT INTO t_news_import (news_id,channel,topic,link,timeinfo,pagesource,pageno) VALUES (s_news_sequence.nextval,?,?,?,to_date(?,'yyyy-mm-dd HH24:MI:SS'),?,?)";
					psforinsert = conn.prepareStatement(sqlStr);
					psforinsert.setString(1, newsInfoModel.getChannel());
					psforinsert.setString(2, newsInfoModel.getTopic());
					psforinsert.setString(3, newsInfoModel.getLink());
					psforinsert.setString(4, newsInfoModel.getDatetime());
					psforinsert.setString(5, newsInfoModel.getSource());
					psforinsert.setInt(6, 0);
					psforinsert.executeUpdate();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					continue;
				} catch(Exception e){
					this.exceptionHandler.writeToLog(e.getMessage());
				}finally{
					psforinsert.close();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} finally{
			
			this.closeConnection();
		}
		
		
	}
	
}
